package com.qianchang.microfinance.view.yfbpay;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.DefutlBankCardBean;
import com.qianchang.microfinance.bean.MyBillBean;
import com.qianchang.microfinance.bean.YBCheckBankCardBean;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.mine.bill.MyBillAdapter;
import com.qianchang.microfinance.widgets.decoration.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/***
 * 易宝支付 2018.8.16
 */

public class YBPayActivity extends BaseActivity{

    @BindView(R.id.tv_forecast_id)
    DTextView tvForeCast;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private MyBillBean.Bean bean;
    public static final String ENTITY_KEY = "MyBillBean_Bean";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yfbpay_layout);
        ButterKnife.bind(this);
        initView();
        initData();
        initRecyclerView();
//        checkBankCard();      //注释摈弃不用2018.10.4
        listBankCankData();
    }


    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_repayment));

    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        bean = (MyBillBean.Bean) bundle.getSerializable(MyBillAdapter.ENTITY_KEY);
        Log.i("wmy", "initData bean = " + bean.toString());
        tvForeCast.setText(bean.getPaymentAmount() + "");
    }

    private static int REQUECODE = 1;
    private static int RESCODE = 2;
    private static int UNTIED_REQUECODE = 3;
    private static int UNTIED_RESCODE = 4;
    private static int ADDCARD_REQUECODE = 5;
    private static int ADDCARD_RESCODE = 6;


    private List<YBCheckBankCardBean> checkBankCardBean = null;
    //用户绑卡列表
    private void checkBankCard(){

        String userId = DMApplication.getInstance().getmUserLoginInfo().getUserId();
        HttpParams params = new HttpParams();
        params.put("userId",userId);

        show();
        DMOKHttpUtils.getInstance().post(YBPayActivity.this, DMConstant.ApiURL.YEPAY_CHECKBANKCARD, params, new RequestCallBack<List<YBCheckBankCardBean>>() {

            @Override
            public void onSuccess(JsonResult<List<YBCheckBankCardBean>> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    checkBankCardBean = response.getObject();
                    if(checkBankCardBean != null && checkBankCardBean.size() > 0){
                        bankCardRvAdapter.setData(checkBankCardBean);
                    }else {
                        checkBankCardBean = null;
                        bankCardRvAdapter.setData(null);
                    }
                }else {
                    checkBankCardBean = null;
                    bankCardRvAdapter.setData(null);
                    ToastUtil.getInstant().show(YBPayActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                checkBankCardBean = null;
                bankCardRvAdapter.setData(null);
                ToastUtil.getInstant().show(YBPayActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }

    private YBCheckBankCardBean ybCheckBankCardBean = null;

    public YBCheckBankCardBean getYbCheckBankCardBean() {
        return ybCheckBankCardBean;
    }
    public void setYbCheckBankCardBean(YBCheckBankCardBean ybCheckBankCardBean) {
        this.ybCheckBankCardBean = ybCheckBankCardBean;
    }



    private BankCardRvAdapter bankCardRvAdapter;
    private void initRecyclerView(){
        List<YBCheckBankCardBean> ybCheckBankCardBean = new ArrayList<YBCheckBankCardBean>();
        bankCardRvAdapter = new BankCardRvAdapter(YBPayActivity.this, ybCheckBankCardBean);
        recyclerView.setAdapter(bankCardRvAdapter);

        RecyclerView.LayoutManager mLayoutManager;
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(mLayoutManager);
        //添加ItemDecoration，item之间的间隔
        int leftRight = dip2px(0);
        int topBottom = dip2px(15);
        recyclerView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
//        recyclerView.addItemDecoration(new SpacesItemDecoration(dip2px(0), dip2px(15), ContextCompat.getColor(getApplicationContext(),R.color.color_edf3f8)));

        bankCardRvAdapter.setOnItemClickListener(new BankCardRvAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {

                if(position == bankCardRvAdapter.getItemCount() -1 ){
                    startActivityForResult(YBPayInputActivity.class,null,ADDCARD_REQUECODE);                //添加银行卡绑定
                }else {
                    YBCheckBankCardBean mybcbc = checkBankCardBean.get(position);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bankCard",mybcbc);
                    bundle.putSerializable(ENTITY_KEY, bean);                     // MyBillBean.Bean
                    startActivityForResult(YBPayDetailsActivity.class,bundle,REQUECODE);        //跳转易宝支付详情页面
                }

            }
        });

        //设置默认支付按钮监听
        bankCardRvAdapter.setOnButClickListener(new BankCardRvAdapter.OnButClickListener() {
            @Override
            public void onButClick(View view, int position) {
                YBCheckBankCardBean mybcbc = checkBankCardBean.get(position);

                //设置默认支付
                setDefultPay(mybcbc);
            }
        });

    }

    public int dip2px(float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics());
    }


    private static int YBPAY_RESULTCODE = 200;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUECODE && resultCode == RESCODE){                                  //支付成功返回结束当前页面
            setResult(YBPAY_RESULTCODE);
            YBPayActivity.this.finish();
        }else if(requestCode == UNTIED_REQUECODE && resultCode == UNTIED_RESCODE){              // 从 YBUntiedBankCardActivity 解绑银行卡成功回调刷新视图
            listBankCankData();
        }else if(requestCode == ADDCARD_REQUECODE && resultCode == ADDCARD_RESCODE){            //绑卡成功从 YBPayInputActivity 页面返回刷新当前视图
            listBankCankData();
        }
    }


    //获取服务银行卡列表数据
    private void listBankCankData(){

        String userId = DMApplication.getInstance().getmUserLoginInfo().getUserId();
        HttpParams params = new HttpParams();
        params.put("userId",userId);

        show();
        DMOKHttpUtils.getInstance().post(YBPayActivity.this, DMConstant.ApiURL.YEPAY_CHECKBANKCARDNEW, params, new RequestCallBack<List<YBCheckBankCardBean>>() {

            @Override
            public void onSuccess(JsonResult<List<YBCheckBankCardBean>> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    checkBankCardBean = response.getObject();
                    if(checkBankCardBean != null && checkBankCardBean.size() > 0){
                        bankCardRvAdapter.setData(checkBankCardBean);
                    }else {
                        checkBankCardBean = null;
                        bankCardRvAdapter.setData(null);
                    }
                }else {
                    checkBankCardBean = null;
                    bankCardRvAdapter.setData(null);
                    ToastUtil.getInstant().show(YBPayActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                checkBankCardBean = null;
                bankCardRvAdapter.setData(null);
                ToastUtil.getInstant().show(YBPayActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    //设置默认支付
    private void setDefultPay(YBCheckBankCardBean bean){

        String requestNo = bean.getRequestNo();
        String userId = DMApplication.getInstance().getmUserLoginInfo().getUserId();

        HttpParams params = new HttpParams();
        params.put("userId",userId);
        params.put("requestno",requestNo);

        show();
        DMOKHttpUtils.getInstance().post(YBPayActivity.this, DMConstant.ApiURL.YEPAY_DEFULTBANKCARD, params, new RequestCallBack<DefutlBankCardBean>() {

            @Override
            public void onSuccess(JsonResult<DefutlBankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    listBankCankData();
                }else {
                    ToastUtil.getInstant().show(YBPayActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(YBPayActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                dismiss();
            }
        });

    }


}
